package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description 商品信息Controller
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 查询所有 当前库存量 小于 库存下限的商品信息
     */
    @PostMapping("/listAlarm")
    public Map<String,Object> listAlarm(){
        List<Goods> list = goodsService.listAlarm();
        Map<String,Object> map =new HashMap<>();
        map.put("rows",list);
        return map;
    }


    /**
     * 删除库存（要判断商品状态 入库、有进货和销售单据的不能删除）
     */
    @PostMapping("/deleteStock")
    public ServiceVO deleteStock(@RequestParam("goodsId") Integer goodsId){
        return goodsService.deleteGoodsById(goodsId);
    }

    /**
     * 添加库存、修改数量或成本价
     */
    @PostMapping("/saveStock")
    public ServiceVO saveStock(@RequestParam("goodsId") Integer goodsId,
                               @RequestParam("inventoryQuantity")Integer inventoryQuantity,
                               @RequestParam("purchasingPrice")Double purchasingPrice){
        goodsService.saveStock(goodsId,inventoryQuantity,purchasingPrice);
        ServiceVO serviceVO = new ServiceVO(100,"请求成功",null);
        return serviceVO;
    }



    /**
     * 无库存商品列表展示（可以根据商品名称或编码查询）
     */
    @PostMapping("/getNoInventoryQuantity")
    public Map<String,Object> getNoInventoryQuantity(@Param("page") Integer page,
                                                     @Param("rows") Integer rows,
                                                     @Param("nameOrCode") String nameOrCode){
        PageHelper.startPage(page,rows);
        List<Goods> goodsList = goodsService.getNoInventoryQuantity(nameOrCode);
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);
        Map<String,Object> map = new HashMap<>();
        map.put("total",pageInfo.getTotal());
        map.put("rows",pageInfo.getList());
        return map;
    }
    /**
     * 有库存商品列表展示（可以根据商品名称或编码查询）
     */
    @PostMapping("/getHasInventoryQuantity")
    public Map<String,Object> getHasInventoryQuantity(@Param("page") Integer page,
                                                      @Param("rows") Integer rows,
                                                      @Param("nameOrCode") String nameOrCode){
        PageHelper.startPage(page,rows);
        List<Goods> goodsList = goodsService.getHasInventoryQuantity(nameOrCode);
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);
        Map<String,Object> map = new HashMap<>();
        map.put("total",pageInfo.getTotal());
        map.put("rows",pageInfo.getList());
        return map;
    }


    /**\
     * 商品删除
     */
    @PostMapping("/delete")
    public ServiceVO delete(@RequestParam("goodsId") Integer goodsId){
        ServiceVO serviceVO = goodsService.deleteGoodsById(goodsId);
        return serviceVO;
    }

    /**
     *商品添加或修改
     */
    @PostMapping("/save")
    public ServiceVO save(@RequestParam("goodsTypeId") Integer goodsTypeId,
                          @RequestParam("goodsCode") String goodsCode,
                          @RequestParam("goodsName") String goodsName,
                          @RequestParam("goodsModel") String goodsModel,
                          @RequestParam("goodsUnit") String goodsUnit,
                          @RequestParam("purchasingPrice") Double purchasingPrice,
                          @RequestParam("sellingPrice") Double sellingPrice,
                          @RequestParam("minNum") Integer minNum,
                          @RequestParam("goodsProducer") String goodsProducer,
                          @RequestParam("remarks") String remarks){
        Goods goods = new Goods();
        goods.setGoodsTypeId(goodsTypeId);
        goods.setGoodsCode(goodsCode);
        goods.setGoodsName(goodsName);
        goods.setGoodsModel(goodsModel);
        goods.setGoodsUnit(goodsUnit);
        goods.setPurchasingPrice(purchasingPrice);
        goods.setSellingPrice(sellingPrice);
        goods.setMinNum(minNum);
        goods.setGoodsProducer(goodsProducer);
        goods.setRemarks(remarks);
        goodsService.saveAndUpdate(goods);
        ServiceVO serviceVO = new ServiceVO(100,"请求成功",null);
        return serviceVO;
    }


    /**
     * 分页查询商品库存信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param codeOrName 商品编码或名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    @PostMapping("/listInventory")
    public String  listInventory(@Param("page") Integer page
            ,@Param("rows") Integer rows
            ,@Param("goodsTypeId") Integer goodsTypeId
            ,@Param("codeOrName") String codeOrName){

        List<Goods> list = goodsService.listInventory(codeOrName,goodsTypeId,page,rows);
        Map<String,Object> map = new HashMap<>();
        map.put("total",list.size());
        map.put("rows",list);
        Gson gson = new Gson();
        String s = gson.toJson(map);
        return s;
    }
//    @PostMapping("/listInventory")
//    public String listInventory(@RequestParam("page") Integer page
//            ,@RequestParam("rows") Integer rows){
//        Gson gson = new Gson();
//        Page<Object> objects = PageHelper.startPage(page, rows);
//        Map<String,Goods> map = goodsService.listInventoryone();
//        String s = gson.toJson(map);
//        return s;
//    }


    /**
     * 分页查询商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param goodsName 商品名称
     * @param goodsTypeId 商品类别ID
     * @return
     */
    @PostMapping("/list")
    public Map<String,Object> list(@Param("page") Integer page
            ,@Param("rows") Integer rows
            ,@Param("goodsTypeId") Integer goodsTypeId
            ,@Param("goodsName") String goodsName){
        PageHelper.startPage(page,rows);
        List<Goods> list = goodsService.list(goodsTypeId,goodsName);
        PageInfo<Goods> pageInfo = new PageInfo<>(list);
        Map<String,Object> map = new HashMap<>();
        map.put("total",pageInfo.getTotal());
        map.put("rows",list);
        return map;
    }


    /**
     * 生成商品编码
     * @return
     */
    @RequestMapping("/getCode")
    @RequiresPermissions(value = "商品管理")
    public ServiceVO getCode() {
        return goodsService.getCode();
    }

    /**
     * 添加或修改商品信息
     * @param goods 商品信息实体
     * @return
     */

    /**
     * 删除商品信息
     * @param goodsId 商品ID
     * @return
     */

    /**
     * 分页查询无库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */


    /**
     * 分页查询有库存商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */


    /**
     * 添加商品期初库存
     * @param goodsId 商品ID
     * @param inventoryQuantity 库存
     * @param purchasingPrice 成本价
     * @return
     */

    /**
     * 删除商品库存
     * @param goodsId 商品ID
     * @return
     */

    /**
     * 查询库存报警商品信息
     * @return
     */

}
